package com.xjnt.frameworks.plugin.sql.criterion.expression;

import com.jfinal.plugin.activerecord.ActiveRecordException;
import com.xjnt.frameworks.plugin.sql.criterion.Criterion;
import com.xjnt.frameworks.plugin.sql.criterion.SqlCriteria;
import com.xjnt.frameworks.plugin.sql.criterion.TypedValue;

public class PropExpression implements Criterion {

	private static final long serialVersionUID = 1L;

	private String leftColumn;
	private String rightColumn;
	private String op;

	public PropExpression(String leftColumn, String rightColumn, String op) {
		this.leftColumn = leftColumn;
		this.rightColumn = rightColumn;
		this.op = op;
	}

	public String toSql(SqlCriteria criteria) {
		StringBuilder sql = new StringBuilder();
		if (criteria.isExists(leftColumn) && criteria.isExists(rightColumn)) {
			sql.append(leftColumn);
			sql.append(op);
			sql.append(rightColumn);
			return sql.toString();
		} else {
			throw new ActiveRecordException("The " + leftColumn + " or "
					+ rightColumn + " is not exists ");
		}
	}

	@Override
	public TypedValue[] getTypedValues(SqlCriteria criterion) {
		return new TypedValue[]{};
	}

}
